﻿using B.S.DataBase.MyProject.Domain.WhereHorse;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Read.API.Applicantions.Command.PurchaseInStroeCommand;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.PurchaseInStroeCommandHandler
{
    /// <summary>
    /// 采购入库明细
    /// </summary>
    public class StorageDetailsCommdHandler : IRequestHandler<StorageDetailsCommd, APIResult<List<PurchaseInRepository>>>
    {


        /// <summary>
        /// 采购入库
        /// </summary>
        private readonly IBaseRepository<PurchaseInRepository> baseRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repository"></param>
        public StorageDetailsCommdHandler(IBaseRepository<PurchaseInRepository> repository)
        {
            this.baseRepository = repository;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<APIResult<List<PurchaseInRepository>>> Handle(StorageDetailsCommd request, CancellationToken cancellationToken)
        {
            APIResult<List<PurchaseInRepository>> result = new APIResult<List<PurchaseInRepository>>();
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();

            var list = baseRepository.GetAll().Result;

            if (request.Id == null)
            {
                list =list.Where(x=>x.Id == request.Id);    
            }

            if(request.DocumentStatus == null)
            {
                list =list.Where(x=>x.DocumentStatus==request.DocumentStatus);
            }

            if(request.BusinessStatus == null)
            {
                list =list.Where(X=>X.BusinessStatus==request.BusinessStatus);
            }

            result.data =list.ToList();

            return Task.FromResult(result);
        }
    }
}
